package controller;

import bean.UrlSave;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import service.UrlGeneratorService;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by YanYang on 2015/12/28.
 */
@Controller
@RequestMapping(value = "/")
public class TinyUrlGeneratorController {
    @Autowired
    public TinyUrlGeneratorController(UrlGeneratorService urlGeneratorService){
        this.urlGeneratorService = urlGeneratorService;
    }
    private UrlGeneratorService urlGeneratorService;


    @RequestMapping(value = "tiny",method = RequestMethod.POST)
    public String tinyUrlGenerator(HttpServletRequest request,
                                    Model model){
        String url = (String)request.getParameter("url");
        url = url.trim();
        if(url != null && !url.equals("")) {
            String tinyUrl = urlGeneratorService.tinyUrlSaveAndGetTinyUrl(url);
            model.addAttribute("tinyUrl",tinyUrl);
        }
        return "redirect:/";
    }

}
